package com.ricohgag.controller.admin;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ricohgag.entity.ShopAd;
import com.ricohgag.service.ShopAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 肖晖
 * @since 2018-08-25
 */
@Controller
@RequestMapping("/admin/shopAd")
public class ShopAdController {
    @Autowired
    private ShopAdService shopAdService;

    /**
     * 通往广告管理的大门
     * @return
     * @throws Exception
     */
    @RequestMapping("/index")
    public ModelAndView toShopAdIndex(String name, Integer current, HttpServletRequest request) throws Exception{

        ModelAndView mav = new ModelAndView("admin/shopAd/index");

        String url = request.getContextPath()+"/admin/shopAd/index";

        if(current == null){
            current = 1;
        }
        Page<ShopAd> page = new Page<>(current, 10);

        IPage<ShopAd> shopAdIPage = shopAdService.selectShopAdPage(name, page);

        System.err.println("name: "+name);
        mav.addObject("name",name);
        mav.addObject("Pager", shopAdIPage);
        mav.addObject("pages", shopAdIPage.getPages());
        mav.addObject("url",url);

        return mav;
    }

    @RequestMapping("/toAdd")
    public ModelAndView toAdd(Integer id) throws Exception{
        ModelAndView mav = new ModelAndView("admin/shopAd/saveOrUpdate");
        if(!"0".equals(id)){
            ShopAd shopAd = shopAdService.seleShopAdOne(id);
            mav.addObject("shopAd", shopAd);
        }
        return mav;
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(ShopAd shopAd, MultipartFile imgFile, HttpServletRequest request) throws Exception{
        System.err.println(shopAd);
        //判断id为空则增加，反之则更改
        if(shopAd.getId() == null){
            shopAdService.insertShopAd(shopAd);
        } else {
            shopAdService.updateShopAd(shopAd);
        }

        return "/common/public/reload";
    }

    @RequestMapping(value = "/delete", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String deleteShopAd(Integer id, HttpServletResponse response) throws Exception{
        Map<String, String> map = new HashMap<>();
        int count = shopAdService.deleteShopAd(id);

        if(count > 0){
            map.put("msg", "删除成功");
        } else {
            map.put("msg", "删除失败");
        }
        response.setCharacterEncoding("utf-8");
        String jsonStr = JSON.toJSONString(map);
        return jsonStr;
    }
}

